﻿using HtmlAgilityPack;
using System.Collections.Generic;
namespace DotNet.Common.Kit
{
    /// <summary>
    /// 解析淘宝HTML页面
    /// </summary>
    public class TaoBaoHtmlHelper
    {
        string _url = "";
        HtmlDocument htmlDoc = new HtmlDocument();

        public string URL
        {
            get { return _url; }
            set { _url = value; }
        }
        public TaoBaoHtmlHelper(string url)
        {
            _url = url;
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem();
            item.URL = _url;
            item.Allowautoredirect = true;
            htmlDoc.LoadHtml(http.GetHtml(item).Html);
        }

        public string DownloadCSS()
        {
            HtmlNodeCollection hnc = htmlDoc.DocumentNode.SelectNodes("//head/link[@rel='stylesheet']");
            //List<string> list = new List<string>();
            string ret="";
            if (hnc != null)
            {
                foreach (HtmlNode node in hnc)
                {
                    string href = node.Attributes["href"].Value.ToString();
                    if (href.Contains("bao/uploaded"))
                        ret = href;
                }
                System.Net.WebClient client = new System.Net.WebClient();
                client.DownloadFile(ret, "xx.css");
            }
            return ret;
        }
    }
}
